package com.itheima.health.listener;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Method;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.io.IOException;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@Component
@RabbitListener(queues = "heima-queue")
public class RabbitConsumer {
    @Autowired
    JavaMailSender javaMailSender;

    @RabbitHandler
    public void process(String msg, Channel channel, Message message) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //msg  123@qq.com,1234
            String[] split = msg.split(",");
            mailMessage.setSubject("验证码");
            mailMessage.setFrom("ld980522@163.com");
            mailMessage.setTo(split[0]);
            mailMessage.setText(split[1]);
            javaMailSender.send(mailMessage);
            //手动签收
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            e.printStackTrace();
            //返回队列
            channel.basicNack(deliveryTag,false,true);
        }
    }
}
